﻿Public Class ExcelWrite
    Inherits ExcelFileWriter(Of String)

    Public Sub New(ByVal rowNumber As Integer, ByVal colNumber As Integer)
        Me.rowNumber = rowNumber
        Me.colNumber = colNumber
    End Sub

    Public Sub New()
        Me.rowNumber = 0
        Me.colNumber = 0
    End Sub

    Public myExcelData(,) As Object

    Private _headers() As Object

    Public Overrides Property Headers() As Object()
        Get
            Return _headers
        End Get
        Set(ByVal value As Object())
            _headers = value
        End Set
    End Property

    Public Overrides Sub FillRowData(ByVal list As List(Of String))

        myExcelData = New Object(RowCount, ColumnCount - 1) {}
        For row As Integer = 0 To RowCount - 2
            For col As Integer = 0 To ColumnCount - 1
                myExcelData(row, col) = list(row * ColumnCount + col)
            Next col
        Next row
    End Sub

    Public Function DGVToExcel(ByVal dgv As DataGridView) As List(Of String)
        Dim myList As List(Of String) = New List(Of String)()
        For i As Integer = 0 To dgv.RowCount - 2
            For j As Integer = 0 To dgv.ColumnCount - 1
                myList.Add(dgv.Rows(i).Cells(j).Value.ToString())
            Next j
        Next i
        colNumber = dgv.ColumnCount
        rowNumber = dgv.RowCount

        Dim header As List(Of Object) = New List(Of Object)()
        For i As Integer = 0 To dgv.ColumnCount - 1
            header.Add(dgv.Columns(i).HeaderText)
        Next i
        Headers = New Object(header.Count - 1) {}
        header.CopyTo(Headers)

        Return myList
    End Function

    Public Overrides ReadOnly Property ExcelData() As Object(,)
        Get
            Return myExcelData
        End Get
    End Property
End Class
